﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.View.ReportQuery
{
    public partial class Report_ZYZXCBB : AbcPageBase
    {

        HisunDBDataContext db = new HisunDBDataContext();
        string CenterIdArray = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());

                DropDownList_Product.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                DropDownList_Product.Items.Clear();
                if (CenterIdArray == "")
                {

                }
                else
                {
                    string[] CenterIDs = CenterIdArray.Split(',');


                    var myProductList = (from a in db.TABC_JS_ProductInProcess
                                         join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                         where CenterIDs.Contains(a.作业中心ID.ToString())
                                         && a.可用 == true
                                         orderby a.产品ID
                                         select new
                                         {
                                             a.产品ID,
                                             b.产品账户名称
                                         }).Distinct();


                    foreach (var myProduct in myProductList)
                    {
                        DropDownList_Product.Items.Add(new ListItem(myProduct.产品ID + "|" + myProduct.产品账户名称, myProduct.产品ID));

                    }

                }
            }
        }




        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p =>  p.停用 == false);


            foreach (var MylistItem in myCenterFilters)
            {

                CenterIdArray = CenterIdArray + "," + MylistItem.作业中心ID.ToString();
               
            }


        }



        protected void Button_QueryLine_Click(object sender, EventArgs e)
        {
            int IsPrinte = 0;

            string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;
  

            string MyProductId = DropDownList_Product.SelectedValue;
            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;
            
            }

             db.单个产品路径缓存(MyAp, MyProductId);


                var MyPathCaches = from n in db.TABC_JS_Product
                                   join m in db.TABC_JS_ProductPathCache
                                   on n.产品账户ID equals m.产品ID into temps 
                                   from temp in temps.Where(p=>p.会计期间ID == MyAp).DefaultIfEmpty()
                                   where n.产品账户ID == MyProductId                            
                                   orderby n.产品账户ID                                   
                                   select new
                                   {
                                       产品ID = n.产品账户ID,
                                       产品账户名称 = n.产品账户名称,
                                       路径 = (temp == null ? "": temp.路径)

                                   };
                if (MyPathCaches.Count() == 0)
                {
                    Response.Write("<script>alert('没有符合当前查询条件的记录');</script>");
                    return;
                }

                string[,] MyGridViewList = new string[MyPathCaches.Count(), 3];
                int i = 0;

                foreach (var MyPathCache in MyPathCaches)
                {


                    string pathstring = MyPathCache.路径;
                    string PathRow = "";
                    if (pathstring != "" && pathstring != null)
                    {
                        string[] MyCenterPIPlists = pathstring.Split(',');

                        string MyCenterlist_lastOne = "";

                        foreach (string MyCenterPIPlist in MyCenterPIPlists)
                        {
                            MyCenterlist_lastOne = MyCenterPIPlist;
                            var MyCenterName = (from a in db.TABC_JS_ProductInProcess
                                                join b in db.TABC_JS_CostCenter
                                                on a.作业中心ID equals b.作业中心ID
                                                where a.中间体ID == MyCenterPIPlist
                                                select new
                                                {
                                                    a.作业中心ID,
                                                    a.中间体ID,
                                                    b.作业中心名称
                                                }).Single();

                            PathRow = PathRow + "→" + "<a href =\"report_zyzxcbb_Detail.aspx?Centerid="+MyCenterName.作业中心ID
                                     + "&PIPid=" + MyCenterName.中间体ID + "&AccountPeriod=" + MyAp +"&IsPrinte="+IsPrinte+ "\">" + MyCenterName.作业中心名称 + "</a>";
                        }

                    }
                    MyGridViewList[i, 0] = MyPathCache.产品ID;
                    MyGridViewList[i, 1] = MyPathCache.产品账户名称;
                    MyGridViewList[i, 2] = PathRow;

                    i++;
                }

                GridView_PathCache.DataSource = ConvertToDataTable(MyGridViewList);
                GridView_PathCache.DataBind();  
            
            

        }


        protected void Button_Printe_Click(object sender, EventArgs e)
        {
            int IsPrinte = 1;

            string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;


            string MyProductId = DropDownList_Product.SelectedValue;
            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;

            }

            db.单个产品路径缓存(MyAp, MyProductId);


            var MyPathCaches = from n in db.TABC_JS_Product
                               join m in db.TABC_JS_ProductPathCache
                               on n.产品账户ID equals m.产品ID into temps
                               from temp in temps.Where(p => p.会计期间ID == MyAp).DefaultIfEmpty()
                               where n.产品账户ID == MyProductId
                               orderby n.产品账户ID
                               select new
                               {
                                   产品ID = n.产品账户ID,
                                   产品账户名称 = n.产品账户名称,
                                   路径 = (temp == null ? "" : temp.路径)

                               };
            if (MyPathCaches.Count() == 0)
            {
                Response.Write("<script>alert('没有符合当前查询条件的记录');</script>");
                return;
            }

            string[,] MyGridViewList = new string[MyPathCaches.Count(), 3];
            int i = 0;

            foreach (var MyPathCache in MyPathCaches)
            {


                string pathstring = MyPathCache.路径;
                string PathRow = "";
                if (pathstring != "" && pathstring != null)
                {
                    string[] MyCenterPIPlists = pathstring.Split(',');

                    string MyCenterlist_lastOne = "";

                    foreach (string MyCenterPIPlist in MyCenterPIPlists)
                    {
                        MyCenterlist_lastOne = MyCenterPIPlist;
                        var MyCenterName = (from a in db.TABC_JS_ProductInProcess
                                            join b in db.TABC_JS_CostCenter
                                            on a.作业中心ID equals b.作业中心ID
                                            where a.中间体ID == MyCenterPIPlist
                                            select new
                                            {
                                                a.作业中心ID,
                                                a.中间体ID,
                                                b.作业中心名称
                                            }).Single();

                        PathRow = PathRow + "→" + "<a href =\"report_zyzxcbb_Detail.aspx?Centerid=" + MyCenterName.作业中心ID
                                 + "&PIPid=" + MyCenterName.中间体ID + "&AccountPeriod=" + MyAp +"&IsPrinte="+IsPrinte+"\">" + MyCenterName.作业中心名称 + "</a>";
                    }

                }
                MyGridViewList[i, 0] = MyPathCache.产品ID;
                MyGridViewList[i, 1] = MyPathCache.产品账户名称;
                MyGridViewList[i, 2] = PathRow;

                i++;
            }

            GridView_PathCache.DataSource = ConvertToDataTable(MyGridViewList);
            GridView_PathCache.DataBind();  
            
            
        }


        #region  转化数据表

        private DataTable ConvertToDataTable(string[,] arr)
        {

            DataTable dataSouce = new DataTable();
            //for (int i = 0; i < arr.GetLength(1); i++)
            //{
            DataColumn newColumn1 = new DataColumn("产品ID", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn1);
            DataColumn newColumn2 = new DataColumn("产品名称", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn2);
            DataColumn newColumn3 = new DataColumn("路径", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn3);
            //}
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                DataRow newRow = dataSouce.NewRow();
                newRow[0] = arr[i, 0];
                newRow[1] = arr[i, 1];
                newRow[2] = arr[i, 2];

                dataSouce.Rows.Add(newRow);
            }
            return dataSouce;

        }

        #endregion




  


    }
}
